package yunquan.wang.base.spring.security;

import java.util.Collection;

import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.vote.RoleVoter;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

public class RoleRulesVoter  extends RoleVoter{
    public int vote(Authentication authentication, Object object, Collection<ConfigAttribute> attributes) {
        int result = ACCESS_ABSTAIN;
        Collection<? extends GrantedAuthority> authorities =authentication.getAuthorities();
        for (ConfigAttribute attribute : attributes) {
            if (this.supports(attribute)) {
                result = ACCESS_DENIED;
                // Attempt to find a matching granted authority
                for (GrantedAuthority authority : authorities) {
                	GrantedAuthorityRulesImpl gra=(GrantedAuthorityRulesImpl)authority;
                    if (attribute.getAttribute().equals(authority.getAuthority())) {
                    	gra.setIsCurentUse(true);
                    	result= ACCESS_GRANTED;
                    }
                    else
                    {
                    	gra.setIsCurentUse(false);
                    }
                }
            }
        }

        return result;
    }
}
